﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using ChatData;
//using ClientServerChat;
using System.IO;

namespace ChatServerOptions
{
    // NOTE: If you change the class name "ChatServer" here, you must also update the reference to "ChatServer" in App.config.
    /// <summary>
    /// Implements IChatServer
    /// </summary>
    [ServiceBehavior(InstanceContextMode =
  InstanceContextMode.Single)]
    public class ChatServer : IChatServer
    {
        TheServer theserver = new TheServer();
            public TheServer Server
            {
                get { return theserver; }
                set { theserver = value; }

            }
            #region IChat Members

            bool IChatServer.UpdateStatus(string user, string status)
            {
                Request newrequest = new Request(RequestType.Status, user);
                newrequest.Status = status;
                Console.WriteLine("Handing status update from " + user);
                theserver.HandleRequest(newrequest);
                return true;
            }
            bool IChatServer.Post(string originIP, string user, string status, string message)
            {
                Request newrequest = new Request(RequestType.Message, user);
                newrequest.IP = originIP;
                newrequest.Status = status;
                newrequest.Mesage = message;
                newrequest.Time = DateTime.Now;
                Console.WriteLine("Handing new Post from " + user);
                theserver.HandleRequest(newrequest);
                return true;
            }

            bool IChatServer.DM(string originIP, string user, string status, string message, string destination)
            {
                Request newrequest = new Request(RequestType.DM, user);
                newrequest.IP = originIP;
                newrequest.Time = DateTime.Now;
                newrequest.Mesage = message;
                newrequest.Destination = destination;
                newrequest.Username = user;
                Console.WriteLine("Handing a Direct message from " + user+" to "+ destination);
                theserver.HandleRequest(newrequest);
                return true;
            }
            void IChatServer.ForwardFile(string user, string destination, string GUID)
            {

                Request newrequest = new Request(RequestType.File, user);
                newrequest.Username = user;
                newrequest.GUID = GUID;
                newrequest.Destination = destination;
                Console.WriteLine(user+ " Fowarded this file "+ GUID +" to "+ destination);
                theserver.HandleFileRequest(newrequest);
                // no need to reverse
            }
            FilePost IChatServer.RequestFile(String user, String GUID)
            {
                Request newrequest = new Request(RequestType.GetFile, user);
                newrequest.GUID = GUID;
                Console.WriteLine(user + " Requested this file " + GUID );
                FileStore thestore = theserver.HandleFileRequest(newrequest);
                FilePost thepost = thestore[0];
                FileInfo file = new FileInfo(thepost.LocalFile);
                thepost.File = System.IO.File.ReadAllBytes(thepost.LocalFile);
                Array.Reverse(thepost.File);// for security
                thepost.LocalFile = file.Name;
                return thepost;
            }
            bool IChatServer.PostFile(string origin, string user, string status, byte[] file, string filename, string destination)
            {
                Array.Reverse(file);//reset original byte order
                Request newrequest = new Request(RequestType.File, user);
                newrequest.IP = origin;
                newrequest.Status = status;
                newrequest.FileName = filename;
                newrequest.File = file;
                newrequest.Destination = destination;
                Console.WriteLine(user + " Sent a file  to " + destination);
                theserver.HandleFileRequest(newrequest);
                return true;
            }

            bool IChatServer.Login(string IP, string port, string username, string password)
            {
                Request newrequest = new Request(RequestType.Login, username);
                newrequest.IP = IP;
                newrequest.Port = port;
                newrequest.Password = password;
                Console.WriteLine(username + " Logged in");
                return theserver.HandleRequest(newrequest);
            }

            void IChatServer.Logout(string user)
            {
                Request newrequest = new Request(RequestType.Logoff, user);
                Console.WriteLine(user + " Logged out ");
                theserver.HandleRequest(newrequest);
            }

            List<DMPost> IChatServer.getDMStream(string user)
            {
                Request newrequest = new Request(RequestType.GetDMStream, user);
                return theserver.HandleDMStreamRequest(newrequest);
            }

            List<DMPost> IChatServer.getDMStreamSince(string user, DateTime time)
            {
                Request newrequest = new Request(RequestType.GetDMStreamSince, user);
                newrequest.Time = time;
                List<DMPost> temp =theserver.HandleDMStreamRequest(newrequest);
                if (temp != null)
                {
                    return temp;
                }
                else return new List<DMPost>();
            }

            List<Post> IChatServer.getStream(string user)
            {
                Request newrequest = new Request(RequestType.GetStream, user);
                List<Post> temp =theserver.HandleStreamRequest(newrequest);
                if (temp!= null)
                {
                    return temp;
                }
                else return new List<Post>();
            }

            List<Post> IChatServer.getUpdatedStream(string user, DateTime time)
            {
                Request newrequest = new Request(RequestType.GetStreamSince, user);
                newrequest.Time = time;
                return theserver.HandleStreamRequest(newrequest);
            }

            List<Record> IChatServer.getActiveUsers()
            {

                return theserver.HandleUserRequest();
            }

            #endregion
        }
    }
